'use strict'
/**
 * 发帖
*/

const postModel = require("../models/post");//发帖模型
const comment = require("../models/comment");//评论模型
const uuid = require("uuid");
const moment = require('moment');
/**
 * 显示发帖页
 * @param {*} ctx 
 * @param {*} next 
 */
exports.showPost =  async (ctx, next) => {
    await ctx.render('post', {
        title: '发帖',
        isLogin   : ctx.cookies.get("uuid") ? true : false
    })
}
/**
 * 显示我的发帖
 */
exports.showMyPost = async (ctx, next) => {
    await ctx.render('myPost', {
        title: '我的发帖',
        isLogin   : ctx.cookies.get("uuid") ? true : false
    })
}
/**
 * 保存发布
 * @param {*} ctx 
 * @param {*} next 
 */
exports.post = async (ctx, next) => {
    const body = ctx.request.body;//获取参数
    body.createTime =  moment(new Date()).format("YYYY-MM-DD HH:mm:ss");
    body.postId = uuid.v1();
    body.isUsing = 1;//默认添加是使用中
    body.updateTime = moment(new Date()).format("YYYY-MM-DD HH:mm:ss");
    body.readNum = 1;//阅读量 初始化1 
    var post = await postModel.save(body);
    if(post){
		ctx.body =  {
			code:"0",
			message: 'success',
		}
	}else{
		ctx.body ={
			code:"-1",
			message: 'error',
		}
    }
    return next;
}
/**
 * 获取所有的列表
 * @param {*} ctx 
 * @param {*} next 
 */
exports.findList = async (ctx, next) => {
    return await postModel.findList({isUsing : 0});
}
/**
 * 根据id查详情
 * @param {*} ctx 
 * @param {*} next 
 */
exports.findDetail = async (params)=>{
    return await postModel.findDetail(params);
}
/**
 * 获取所有我的发帖
 * @param {*} ctx 
 * @param {*} next 
 */
exports.getMyPost = async (ctx,next) =>{
    const uuid = ctx.query.uuid;
    var list = await postModel.findList({isUsing : 1,uuid : uuid})
    if(list){
        ctx.body =  {
			code:"0",
            message: 'success',
            data: list 
		}
    }else{
        ctx.body =  {
			code:"-1",
			message: '您还未发贴，请先发帖',
		}
    }
}
/**
 * 删除选中的项
 * 支持多选删除
 * @param {*} ctx 
 * @param {*} next 
 */
exports.delete = async (ctx, next) => {
    var ids = ctx.query.ids.split(",");
    if(ids.length == 0){
        ctx.body = {
            code:"-1",
            message: '请选择删除的列'
        }
    }else {
        var flag = false;
        var errIds = [];
        for(var i = 0;i < ids.length;i ++){
            /**
             * 将删除标志改为 1 
             * 当前删除
             * @param {*} find 
             * @param {*} params 
             */
            let delTag = await postModel.updateData({postId : ids[i]},{isUsing : 0,updateTime : moment(new Date()).format("YYYY-MM-DD HH:mm:ss")},{ multi: true },false,true);
            if(delTag.n > 0){
                flag = true;
            }else {
                flag = false;
                errIds.push(element);
            }
        }
        if(flag){
            ctx.body =  {
                code:"0",
                message: '删除成功',
                ids: ids
            }
        }else{
            ctx.body =  {
                code:"-1",
                message: '删除成功',
                ids: errIds
            }
        }
    }
}
/**
 * 保存评论
 * @param {*} ctx 
 * @param {*} next 
 */
exports.saveComment = async (ctx, next) => {
    var params = ctx.request.body;
    params.commentUuid = uuid.v1();
    var res = await comment.saveComment(params);
    if(res){
        ctx.body =  {
			code:"0",
            message: 'success'
		}
    }else{
        ctx.body =  {
			code:"-1",
			message: '评论失败',
		}
    }
}
/**
 * 点赞
 * @param {*} ctx 
 * @param {*} next 
 */
exports.like = async (ctx, next) => {
    var params = ctx.request.body,likes = [],flag = true;
    var postRecord = await postModel.findLike(params.postId);
    if(postRecord.length > 0){
        likes = postRecord[0].like;
        if(! likes instanceof Array){//强制转换成数组
            likes = [];
        }
    }
    var index = likes.indexOf(params.uuid);
    if( index >= 0){
        flag = false;//取消点赞
        likes.splice(index,1);
    }else{
        flag = true;
        likes.push(params.uuid);
    }
    var res = await postModel.updateData({postId : params.postId},{like : likes});
    if(res){
        ctx.body =  {
            code    : "0",
            flag    : flag,
            message : 'success'
		}
    }else{
        ctx.body =  {
			code:"-1",
			message: '点赞失败',
		}
    }
}